package com.ww.web;

import com.alibaba.fastjson.JSON;
import com.ww.dao.MenuDao;
import com.ww.dao.impl.MenuDaoImpl;
import com.ww.entity.Menu;
import com.ww.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/menu/*")
public class MenuServlet extends HttpServlet {
    private MenuDao menuDao=new MenuDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String baseURL = req.getContextPath();//获取项目的基本路径
        String uri = req.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        JSONBean jsonBean=new JSONBean();
        if ("ajaxMenu.do".equals(path)){
            List<Menu> menuList = menuDao.findAll();
            jsonBean.setData(menuList);
            BaseServlet.printJSON(resp,jsonBean); //输出json内容
        }else {
            throw new RuntimeException("请求错误");
        }
    }
}
